import router from "@/router";
import { toast } from "vant";
import { registers, logins, getperons, sendcode, forget } from "@/server/login";
import { setStorage, setSession, removeSession, removeStorage } from "@/utils/common";
export default {
  state: () => ({}),
  mutations: {},
  actions: {
    login({ commit }, data) {
      removeSession('notice')
      return new Promise((resolve, reject) => {
        logins(data).then((res) => {
          if (res.code == 0) {
            setStorage("token", res.data.access_token);
            getperons().then((res) => {
              setStorage("infpo", res.data);
              resolve(true);
            });
          } else {
            toast(res.msg);
          }
        }).catch(err=>{
          resolve(false);
        })
      });
    },
    forget({ commit }, data) {
      return new Promise((resolve, reject) => {
        forget(data).then((res) => {
          if (res.code == 0) {
              resolve();
          } else {
            toast(res.msg);
          }
        });
      });
    },
    sendcode({ commit }, data) {
      return new Promise((resolve, reject) => {
        sendcode(data).then((res) => {
          if (res.code == 0) {
            resolve(res.data.key);
          } else {
            toast(res.msg);
          }
        });
      });
    },
    registers({ commit }, data) {
      return new Promise((resolve, reject) => {
        registers(data).then((res) => {
          if (res.code == 0) {
            setStorage("token", res.data.access_token);
            getperons().then((res) => {
              setStorage("infpo", res.data);
              resolve(true);
            });
          } else {
            resolve(false);
            toast(res.msg);
          }
        }).catch(err=>{
          resolve(false);
        })
      });
    },
    loginout() {
      // Toast("Exit succeeded");
      removeStorage("infpo");
      removeStorage("token");
      router.replace("/login");
    },
    getperons({ commit }, data) {
      return new Promise((resolve, reject) => {
        getperons().then((res) => {
          if (res.code == 0) {
            // addCookie("perons",res.data)
            setStorage("infpo", res.data);
            resolve();
          }else{
            toast(res.msg);
          }
        });
      });
    },
  },
  getters: {},
};
